package com.sssre.lttcloud.config.controller;

import com.sssre.lttcloud.api.entity.Config;
import com.sssre.lttcloud.common.result.Result;
import com.sssre.lttcloud.config.service.ConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 系统配置控制器
 */
@RestController
@RequestMapping("/config")
@CrossOrigin
public class ConfigController {

    @Autowired
    private ConfigService configService;

    /**
     * 分页查询配置
     */
    @GetMapping("/page")
    public Result getConfigPage(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String configName,
            @RequestParam(required = false) String configKey) {
        return configService.getConfigPage(pageNum, pageSize, configName, configKey);
    }

    /**
     * 查询所有配置
     */
    @GetMapping("/list")
    public Result getAllConfigs() {
        return configService.getAllConfigs();
    }

    /**
     * 根据ID查询配置
     */
    @GetMapping("/{id}")
    public Result getConfigById(@PathVariable Long id) {
        return configService.getConfigById(id);
    }

    /**
     * 根据配置键查询配置值
     */
    @GetMapping("/value/{configKey}")
    public Result getConfigValue(@PathVariable String configKey) {
        return configService.getConfigValue(configKey);
    }

    /**
     * 新增配置
     */
    @PostMapping
    public Result addConfig(@Valid @RequestBody Config config) {
        return configService.addConfig(config);
    }

    /**
     * 修改配置
     */
    @PutMapping
    public Result updateConfig(@Valid @RequestBody Config config) {
        return configService.updateConfig(config);
    }

    /**
     * 删除配置
     */
    @DeleteMapping("/{id}")
    public Result deleteConfig(@PathVariable Long id) {
        return configService.deleteConfig(id);
    }

    /**
     * 批量删除配置
     */
    @DeleteMapping("/batch")
    public Result deleteConfigs(@RequestBody List<Long> ids) {
        return configService.deleteConfigs(ids);
    }

    /**
     * 检查配置键是否唯一
     */
    @GetMapping("/check")
    public Result checkConfigKeyUnique(
            @RequestParam String configKey,
            @RequestParam(required = false) Long id) {
        return configService.checkConfigKeyUnique(configKey, id);
    }

    /**
     * 刷新配置缓存
     */
    @PostMapping("/refresh")
    public Result refreshConfigCache() {
        return configService.refreshConfigCache();
    }
} 